package com.blast.manager.delay

import android.view.KeyEvent
import androidx.lifecycle.Observer
import com.blast.manager.R
import com.blast.manager.delay.event.KeyDownEvent
import com.blast.manager.delay.presenter.*
import com.blast.manager.pop.DelayMorePop
import com.blast.manager.pop.DeleteTipPop
import com.wxl.common.base.AbsActivity
import com.wxl.common.life.LifecycleManager
import com.wxl.common.life.ViewModelQuick
import com.wxl.common.room.entity.LogEntity
import com.wxl.common.serialport.SerialPortManager
import com.wxl.common.util.ObservableUtil
import com.wxl.common.util.Toll
import com.wxl.common.viewmodel.AbsPresenter
import com.wxl.common.wiget.BatchUpdateLeiGuanPop
import io.reactivex.functions.Action
import kotlinx.android.synthetic.main.activity_delay_layout.*

class DelayActivity : AbsActivity(){

    private var isCanDownBackEvent = true
    private var mDelayMorePop:DelayMorePop?=null

    override fun getCreateContentViewId(): Int {
        return R.layout.activity_delay_layout
    }

    override fun onCreateViewChanged() {
        isCheckAppUpgrade = false
        SerialPortManager.openOnlyPort(object : SerialPortManager.Companion.OpenPortCallback{
            override fun onOpenPort(state: Int) {
                observer(DelayCjqPortPresenter::class.java)
            }
        })

        setTopTitle("延时方案")

        val mKeyDownEvent = KeyDownEvent()
        mKeyDownEvent.observe(this, Observer {
            isCanDownBackEvent = it!!.isCanBack
        })
        LifecycleManager.manager.registerLiveData(mKeyDownEvent,lifecycle)

        delayMoreBtn.setOnClickListener {
            showMorePop()
        }

        delayUpdateBtn.setOnClickListener {
            ViewModelQuick.get(this@DelayActivity,DataHandlePresenter::class.java).updateSelectPosition()
        }

        delaySortBtn.setOnClickListener {
            ViewModelQuick.get(this@DelayActivity,DataHandlePresenter::class.java).sortData()
        }

        delayInsertBtn.setOnClickListener {
            ViewModelQuick.get(this@DelayActivity,DataHandlePresenter::class.java).insertItem()
        }

        observer(ShowDelayDataPresenter::class.java)
        observer(OnlineImportPresenter::class.java)
        observer(DataHandlePresenter::class.java)
        observer(CheckAuthPresenter::class.java)
    }


    private fun showMorePop(){
        mDelayMorePop = DelayMorePop.show(delayMoreBtn,object : ObservableUtil.Companion.OnNextCallback<Int>(){
            override fun onNext(t: Int) {
                when (t) {
                    0 -> {//批量修改
                        addLog("批量修改","点击批量修改")
                        val data = ViewModelQuick.get(
                            this@DelayActivity,
                            ShowDelayDataPresenter::class.java
                        ).getData()
                        if(data.isEmpty()){
                            Toll.toll("没有雷管数据")
                            return
                        }
                        BatchUpdateLeiGuanPop.show(this@DelayActivity,ViewModelQuick.get(this@DelayActivity,ShowDelayDataPresenter::class.java).getData())
                    }
                    1 -> {//在线导入
                        addLog("在线导入","点击在线导入")
                        ViewModelQuick.get(this@DelayActivity,ShowDelayDataPresenter::class.java).resetStartImport()
                        ViewModelQuick.get(this@DelayActivity,OnlineImportPresenter::class.java).startImport()
                    }
                    2 -> {//清除方案
                        addLog("清除方案","点击清除方案")
                        DeleteTipPop.show(this@DelayActivity, Action {
                            ViewModelQuick.get(this@DelayActivity,ShowDelayDataPresenter::class.java).clearData()
                        })
                    }
                    3 -> {//删除
                        ViewModelQuick.get(this@DelayActivity,DataHandlePresenter::class.java).deleteSelectPosition()
                    }
                }
            }
        })
    }

    override fun onKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
        val b = ViewModelQuick.get(this, ShowDelayDataPresenter::class.java).onKeyDown(keyCode, event)
        if(b){
            return true
        }
        if(!isCanDownBackEvent){
            //mDelayMorePop?.onKeyDown(keyCode, event)
            return false
        }
        if(keyCode == KeyEvent.KEYCODE_BACK && isCanDownBackEvent){
            ViewModelQuick.get(this,CheckAuthPresenter::class.java).startAuthCheck()
        }
        if(keyCode == KeyEvent.KEYCODE_F1){
            delayUpdateBtn.performClick()
        }
        if(keyCode == KeyEvent.KEYCODE_F2){
            delaySortBtn.performClick()
        }
        if(keyCode == KeyEvent.KEYCODE_F3){
            delayInsertBtn.performClick()
        }
        if(keyCode == KeyEvent.KEYCODE_F4){
            delayMoreBtn.performClick()
        }
        return false
    }


   private fun <T : AbsPresenter> observer(clazz: Class<T>){
        ViewModelQuick.observer(this, clazz,delayRootView)
    }

    override fun onDestroy() {
        super.onDestroy()
        SerialPortManager.cancelIntervalWork()
       // SerialPortManager.close()
       // SerialPortManager.closePower()
    }

    fun addLog(action: String, remark: String) {
        LogEntity.addLog(
            action,
            "",
            "延时方案",
            ViewModelQuick.get(this,ShowDelayDataPresenter::class.java).getTotal(),
            0.0,
            0.0,
            "",
            0,
            0,
            "",
            remark
        )
    }

    fun addLog(action: String, p: Int, remark: String) {
        LogEntity.addLog(
            action,
            "",
            "延时方案",
            ViewModelQuick.get(this,ShowDelayDataPresenter::class.java).getTotal(),
            0.0,
            0.0,
            "",
            p,
            0,
            "",
            remark
        )
    }

}